<?php
namespace api\modules\ua\models;

use Yii;
use DateTime;
use DateTimeZone;

class Qunmai
{
    public $cache_key_p_ver = 'uaPromotionVersion';
    public $cache_key_g_ver = 'uaGoodsVersion';
    public $cache_key_promotion = 'uaPromotionData';
    public $cache_key_goods = 'uaGoodsData';
    public $cache_expire = 2592000; // 3600*24*30;
    public $storeCode;

    // 获取版本
    public function buildData($storeCode){
        $v = $this->qmVersions();
        $this->storeCode = $storeCode;

//        $data['version']     = $v;
        $data['wxcode']      = $this->qmQrcode();
//        $data['promotion']   = $this->handlePromotion($v->homePageVersion);
//        $data['goods']       = $this->handleGoods($v->redemptionModuleVersion);

        $data['promotion']   = $this->getPromotionData();
        $data['goods']       = $this->getGoodsData();

//        $data['global']    = $this->handleGoods($v->redemptionModuleVersion);

        // 获取活动数据
        $data['version']   = ['promotion'=>$v->homePageVersion, 'goods'=>$v->redemptionModuleVersion, 'uv'=>$v->mockNumber];
//        $data['promotion'] = $this->handlePromotion($v->homePageVersion);
//        $data['goods']     = $this->handleGoods($v->redemptionModuleVersion);
//        $data['global']    = $this->handleGoods($v->redemptionModuleVersion);

        return $data;
    }

    protected function getVersions(){
        $url = Yii::$app->params['ua']['conf']['qmApi']['version'];
        return $this->getData($url);
    }


    // 获取promotion数据
    protected function handlePromotion($newVersion){
        $cache = Yii::$app->cache;
        $version = $cache->get($this->cache_key_p_ver);

        if($version===false || $newVersion!==$version){
            // 版本不一致，获取新版本数据
            $data = $this->getPromotionData();
            $cache->set($this->cache_key_p_ver, $newVersion, $this->cache_expire);
            $cache->set($this->cache_key_promotion, $data, $this->cache_expire);
        }else{
            // 版本一致，获取缓存数据
            $data = $cache->get($this->cache_key_promotion);
        }

        return $data;
    }

    // 比较时间
    protected function compareTime($startAt, $endAt) {
        // 将 ISO 8601 格式的时间转换为 DateTime 对象，并设置为 UTC
        $start = new DateTime($startAt, new DateTimeZone('UTC'));
        $end = new DateTime($endAt, new DateTimeZone('UTC'));

        $currentTimeShanghai = new DateTime('now', new DateTimeZone('Asia/Shanghai'));
        $current = $currentTimeShanghai->setTimezone(new DateTimeZone('UTC'));

        return $current>=$start && $current<=$end;
    }

    protected function getTime($startAt, $endAt) {
        // 将 ISO 8601 格式的时间转换为 DateTime 对象，并设置为 UTC
        $start   = new DateTime($startAt);
        $end     = new DateTime($endAt);
//        $end = $_end->setTimezone(new DateTimeZone('Asia/Shanghai'));
        $current = new DateTime('now', new DateTimeZone('Asia/Shanghai'));

        return [$start,$end,$current,$current>=$start && $current<=$end];
    }


    // 获取promotion数据
    protected function handleGoods($newVersion){
        $cache = Yii::$app->cache;
        $version = $cache->get($this->cache_key_g_ver);

        if($version===false || $newVersion!==$version){
            // 版本不一致，获取新版本数据
            $data = $this->getGoodsData();

            // 这里要缓存数据 ===========
            $cache->set($this->cache_key_g_ver, $newVersion, $this->cache_expire);
            $cache->set($this->cache_key_goods, $data, $this->cache_expire);
        }else{
            // 版本一致，获取缓存数据
            $data = $cache->get($this->cache_key_goods);
        }
        return $data;
    }


    // 请求数据
    protected function getData($url){
        $token= Yii::$app->params['ua']['conf']['qmApi']['token'];
        $curl = new Request();
        $response = $curl->curlGet($url,$token);
        $json = json_decode($response);

        // 这里需要根据最终接口格式进行调整
        return $json->data->data;
    }

    protected function qmData($url,$params=[]){
        $headers= Yii::$app->params['ua']['conf']['qmApi']['headers'];
        $curl = new Request();
        $response = $curl->qmGet($url,$headers,$params);
        $json = json_decode($response);

        // 这里需要根据最终接口格式进行调整
        return $json;
    }

    // 群麦版本
    protected function qmVersions(){
        $url = Yii::$app->params['ua']['conf']['qmApi']['versions'];
        return $this->qmData($url);
    }

    protected function qmQrcode(){
        $url = Yii::$app->params['ua']['conf']['qmApi']['wxcode'];
        return $this->qmData($url,['storeCode'=>$this->storeCode]);
    }

    // 向群脉请求promotion数据
    protected function getPromotionData(){
        $url = Yii::$app->params['ua']['conf']['qmApi']['promotion'];
        return $this->qmData($url,['storeCode'=>$this->storeCode]);
    }

    // 向群脉请求goods数据
    protected function getGoodsData(){
        $url = Yii::$app->params['ua']['conf']['qmApi']['goods'];
        return $this->qmData($url,['storeCode'=>$this->storeCode]);
    }
}
